package main

import (
	"fmt"
)

func f(x person) {
	x.gender = "女"
}

func f2(x *person) {
	(*x).gender = "女"
}

type person struct {
	name   string
	gender string
	age    int
}

// 构造函数，返回结构体
func newPerson(name string, age int) person {
	return person{
		name: name,
		age:  age,
	}
}

// 构造函数，返回结构体指针
func newPerson2(name string, age int) *person {
	return &person{
		name: name,
		age:  age,
	}
}

// 方法是作用于特定类型的函数
func (p person) work() {
	fmt.Printf("%s is working\n", p.name)
}

func (p person) guonian() {
	p.age++
}

func (p *person) zhenguonian() {
	p.age++
}

type myInt int

func (m myInt) hello() {
	fmt.Printf("我是一个int")
}

func main() {
	// 1.结构体-声明实例化
	var p person
	p.name = "robbin"
	p.gender = "男"
	f(p)
	fmt.Println(p.gender)
	f2(&p)
	fmt.Println(p.gender)
	fmt.Printf("%p\n", &p)

	// 2.new函数实例化
	var p2 = new(person)
	p2.name = "ldd"
	fmt.Printf("%T\n", p2)
	fmt.Printf("%p\n", p2)

	// 3.键值对初始化
	var p4 = person{
		name: "ldd",
		age:  10,
	}

	p4 = person{
		"ldd2",
		"男",
		10,
	}
	fmt.Println(p4.name)

	// 4.使用构造函数实例化
	p3 := newPerson2("robbin", 18)
	fmt.Printf("p3:%T\n", p3)
	fmt.Printf("p3:%p\n", p3)

	p3.work()
	fmt.Println(p3.age)
	p3.zhenguonian()
	fmt.Println(p3.age)

	// 匿名结构体
	var a = struct {
		x int
		y int
	}{10, 20}
	fmt.Println(a.x)

	// myInt
	mi := myInt(100)
	mi.hello()
}
